package com.traveling.hztraveling.Task;

import com.traveling.hztraveling.entity.po.HzCulture;
import com.traveling.hztraveling.service.IHzCultureService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDate;
import java.util.List;

@Component
@Slf4j
public class ExpireTask {

    @Autowired
    private IHzCultureService hzCultureService;

     // 处理过期的订单（pending->expire）
    @Scheduled(cron = "0 0 0 * * *")
//    @Scheduled(cron = "0/5 * * * * ?")
    public void expireTask() {
        log.info("开始执行定时任务");
        List<HzCulture> list = hzCultureService.lambdaQuery()
                .eq(HzCulture::getStatus, "pending")
                .list();
//        log.info("待处理的订单：{}", list);
        for (HzCulture hzCulture : list) {
            if (hzCulture.getDate().isBefore(LocalDate.now())) {
                hzCulture.setStatus("expire");
                hzCultureService.updateById(hzCulture);
                 log.info("订单{}已过期", hzCulture.getId());
                 log.info("定时任务执行，已更新：{}", hzCulture);
            }
        }

    }

}
